package lambdaTest;

/*
 * Author: JacksonCoder
 * FileName: test
 * Version: 1.0.0
 * Date: 2024/12/16
 */

import java.util.*;
import java.util.stream.Collectors;

@FunctionalInterface
interface GreetingService {
    void sayMessage(String message);
}

class Person {
    String name;

    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return name + " ( " + age + " )";
    }
}

public class test {
    public static void main(String[] args) {
        // 使用  lambda 创建线程
        Thread thread = new Thread(() -> System.out.println("Thread is running"));
        thread.start();

        printForeach();
        selfDefend();
        LambdaMapExample();
        LambdaStreamExample();
        LambdaComparatorExample();
        LambdaCustomInterfaceExample();
        LambdaRunnableExample();
    }

    public static void LambdaRunnableExample() {
        Runnable task = () -> {
            for (int i = 1; i <= 5; i++) {
                System.out.println("Runnable is running " + i);
            }
        };
        Thread thread = new Thread(task);
        thread.start();
    }

    public static void LambdaCustomInterfaceExample() {
        // 使用lambda 实现接口方法
        GreetingService greetingService = message -> System.out.println("Hello, " + message);
        greetingService.sayMessage("Java");
    }

    public static void LambdaComparatorExample() {
        List<Person> people = new ArrayList<>();
        people.add(new Person("Alice", 30));
        people.add(new Person("Bob", 310));
        people.add(new Person("Charlie", 303));
        people.add(new Person("David", 130));

        // 按年龄排序
        people.sort((p1, p2) -> Integer.compare(p1.age, p2.age));
        people.forEach(System.out::println);
    }

    public static void LambdaStreamExample() {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
        List<String> upperCaseNames = names.stream()
                .map(String::toUpperCase)
                .collect(Collectors.toList());
        System.out.println(upperCaseNames);


        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
        long count = numbers.stream()
                .filter(n -> n % 2 == 0)
                .count();
        System.out.println("Even numbers count: " + count);
    }

    public static void LambdaMapExample() {
        Map<String, Integer> map = new HashMap<>();
        map.put("Apple", 1);
        map.put("Banana", 10);
        map.put("Orange", 100);
        map.put("Pair", 1000);

        // 遍历 Map
        map.forEach((k, v) -> System.out.println("Key: " + k + ", Value: " + v));

        // 条件操作
        map.forEach((key, value) -> {
            if (value > 15) {
                System.out.println(key + " is expensive!");
            }
        });

    }

    public static void selfDefend() {
        List<String> names = Arrays.asList("Alice", "Charlie", "Bob");
        // 按字符串长度排序
        Collections.sort(names, (a, b) -> Integer.compare(a.length(), b.length()));
        System.out.println(names);
    }

    public static void printForeach() {
        List<String> names = Arrays.asList("Tom", "Jerry", "Alice", "Bob");
        names.forEach(name -> System.out.println(name));

        List<String> nameList = Arrays.asList("Alice", "Bob", "CHarlie", "David");
        // 过虑掉名字长度小于3的元素
        List<String> filteredNames = nameList.stream()
                .filter(name -> name.length() > 3)
                .collect(Collectors.toList());
        System.out.println(filteredNames);
    }

}
